package com.css.fxfzysx.modules.vabStructure.controller;

import cn.hutool.core.io.FileUtil;
import com.css.fxfzysx.base.response.RestResponse;
import com.css.fxfzysx.modules.cztDataMange.entity.CpvAnalysisEntity;
import com.css.fxfzysx.modules.cztDataMange.entity.CpvInfoEntity;
import com.css.fxfzysx.modules.vabStructure.param.CpvInfoParam;
import com.css.fxfzysx.modules.vabStructure.param.CustomVulnerabilityParam;
import com.css.fxfzysx.modules.vabStructure.service.PsvAnalysisService;
import com.css.fxfzysx.modules.vabStructure.vo.CpvInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 概率地震易损性分析
 */
@RestController
@RequestMapping("/psvAnalysis")
public class PsvAnalysisController {

    @Autowired
    private PsvAnalysisService psvAnalysisService;

//    @GetMapping("/queryCpvAnalysis")
//    public RestResponse queryCpvAnalysis(CpvAnalysisEntity cpvAnalysisEntity) {
//        return null;
//    }

    /**
     * 导入概率地震易损性分析数据
     */
    @PostMapping("/uploadPsaData")
    public RestResponse uploadPsaData(@RequestParam("file") MultipartFile file,@RequestParam("type") String type) {
        if (!FileUtil.getSuffix(file.getOriginalFilename()).equals("xls") && !FileUtil.getSuffix(file.getOriginalFilename()).equals("xlsx")) {
            return RestResponse.fail("文件格式不支持!");
        }
        return psvAnalysisService.uploadPsaData(file,type);
    }

    /**
     * 导入mc、βc 数据
     */
    @PostMapping("/uploadMcData")
    public RestResponse uploadMcData(@RequestParam MultipartFile file) {
        if (!FileUtil.getSuffix(file.getOriginalFilename()).equals("xls") && !FileUtil.getSuffix(file.getOriginalFilename()).equals("xlsx")) {
            return RestResponse.fail("文件格式不支持!");
        }
        return psvAnalysisService.uploadMcData(file);
    }

    /**
     * 导入地震动时程数据
     * @param file
     * @return
     */
    @PostMapping("/importSeismicTimeHistory")
    public RestResponse importSeismicTimeHistory(@RequestParam MultipartFile file) {
        if (!FileUtil.getSuffix(file.getOriginalFilename()).equals("xls") && !FileUtil.getSuffix(file.getOriginalFilename()).equals("xlsx")) {
            return RestResponse.fail("文件格式不支持!");
        }
        return psvAnalysisService.importSeismicTimeHistory(file);
    }

    /**
     * 导出地震动参数数据
     */
    @GetMapping("exportExcel")
    public void exportExcel(HttpServletResponse response,String cpviId){
        psvAnalysisService.exportExcel(response,cpviId);
    }

    /**
     * 计算
     */
    @PostMapping("/countCpvAnalysis")
    public RestResponse countCpvAnalysis(@RequestBody CpvInfoParam param) {
        RestResponse restResponse = psvAnalysisService.countCpvAnalysis(param);
        if (restResponse.getCode() == 200) {
            restResponse.setMessage("计算成功！");
        } else if (restResponse.getCode() == 500) {
            restResponse.setMessage("计算失败！");
        }
        return restResponse;
    }

    /**
     * 保存概率地震易损性信息
     */
    @PostMapping("/saveCpvInfoEntity")
    public RestResponse saveCpvInfoEntity(@RequestBody CpvInfoParam param) {
        RestResponse restResponse = psvAnalysisService.saveCpvInfoEntity(param);
        if (restResponse.getCode() == 200) {
            restResponse.setMessage("保存成功！");
        } else if (restResponse.getCode() == 500) {
            restResponse.setMessage("保存失败！");
        }
        return restResponse;
    }

    /**
     * 保存
     */
    @PostMapping("/saveCpvAnalysis")
    public RestResponse saveCpvAnalysis(List<CpvAnalysisEntity> list) {
        RestResponse restResponse = psvAnalysisService.saveCpvAnalysis(list);
        if (restResponse.getCode() == 200) {
            restResponse.setMessage("保存成功！");
        } else if (restResponse.getCode() == 500) {
            restResponse.setMessage("保存失败！");
        }
        return restResponse;
    }

    /**
     * 保存
     */
    @PostMapping("/saveCpvResultEntity")
    public RestResponse saveCpvResultEntity(List<Map<String,Object>> mapList) {
        RestResponse restResponse = psvAnalysisService.saveCpvResultEntity(mapList);
        if (restResponse.getCode() == 200) {
            restResponse.setMessage("保存成功！");
        } else if (restResponse.getCode() == 500) {
            restResponse.setMessage("保存失败！");
        }
        return restResponse;
    }

    /**
     * 查询
     */
    @GetMapping("/getById")
    public RestResponse getById(@RequestParam String id) {
        RestResponse restResponse = psvAnalysisService.getById(id);
        if (restResponse.getCode() == 200) {
            restResponse.setMessage("查询成功！");
        } else if (restResponse.getCode() == 500) {
            restResponse.setMessage("查询失败！");
        }
        return restResponse;
    }

    /**
     * 分页查询
     * @param cpvInfoEntity
     * @return
     */
    @GetMapping("queryCpvAnalysis")
    public RestResponse queryCpvAnalysis(CpvInfoEntity cpvInfoEntity,HttpServletRequest request){
        return psvAnalysisService.queryCpvAnalysis(cpvInfoEntity,request);
    }

    /**
     * 移入，移出基础库
     */
    @GetMapping("moveBasicLibrary")
    public RestResponse moveBasicLibrary(CustomVulnerabilityParam param) {
        return psvAnalysisService.moveBasicLibrary(param);
    }
}
